<script setup>
import PageContainer from '@/components/PageContainer.vue';
import { artDelChannelService, artGetChannelList } from '@/api/article'
import { ref, onMounted } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import ChannelEdit from '@/components/ChannelEdit.vue';
import { ElMessage } from 'element-plus';
// 获取文章分类
const channelList = ref([])
const getChannelList = async () => {
  loading.value = true
  const res = await artGetChannelList()
  loading.value = false
  channelList.value = res.data.data
}
onMounted(() => {
  getChannelList()
})
// loading效果
const loading = ref(false)

const dialog = ref()
// 编辑
const onEditChannel = (row) => {
  dialog.value.open(row)
  // console.log(row)
}
// 删除
const onDelChannel = async (row) => {
  await ElMessageBox.confirm('你确认删除该分类信息吗？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await artDelChannelService(row.id)
  // console.log(row)
  ElMessage({ type: 'success', message: '删除成功' })
  getChannelList()
}
// 添加
const onAddChannel = () => {
  dialog.value.open({})
}

// // 数据回显
// const onSuccess = () => {
//   getChannelList()
// }
</script>

<template>
  <PageContainer title="文章分类">
    <template #btn>
      <el-button @click="onAddChannel" type="primary">添加分类</el-button>
    </template>
    <template #main>
      <el-table :data="channelList" style="width: 100%" v-loading="loading">
        <el-table-column label="序号" width="100" type="index"> </el-table-column>
        <el-table-column label="分类名称" prop="cate_name"></el-table-column>
        <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
        <el-table-column label="操作" width="100">
          <template #default="scope">
            <el-button :icon="Edit" circle plain type="primary" @click="onEditChannel(scope.row)"></el-button>
            <el-button :icon="Delete" circle plain type="danger" @click="onDelChannel(scope.row)"></el-button>
          </template>
        </el-table-column>
        <template #empty>
          <el-empty description="没有数据" />
        </template>
      </el-table>
      <ChannelEdit @success="getChannelList" ref="dialog"></ChannelEdit>
    </template>
  </PageContainer>

</template>
